var express = require('express');
var router = express.Router();
var mongoose = require("mongoose")
var {typeModel,clothesModel} = require('../model/model')

//添加类型信息
router.post('/add_type', function(req, res, next) {
  let body = req.body
  typeModel.create(body)
  res.send({
   code:200,
   msg:'添加衣服类型成功'
  })
 });

 //添加衣服信息
router.post('/add_clothes', function(req, res, next) {
  let body = req.body
  clothesModel.create(body)
  res.send({
   code:200,
   msg:'添加衣服成功'
  })
 });


//衣服类型查询
router.get('/type_list', async (req, res, next) =>{
  let data = await typeModel.find()
  res.send({
    code:200,
    data:data
  })
 });

//衣服信息查询
router.get('/clothes_list',async (req,res)=>{
  let {name} = req.query
  let regName = new RegExp(name)
  let {tid} = req.query
  if(name){
    data = await clothesModel.find({tid},{name:regName}).populate('tid')
    res.send({
      msg:"成功",
      data,
    })
  }
  else{
    data = await clothesModel.find({tid}).populate('tid')
    res.send({
      msg:"成功",
      data,
    })
  }
})

module.exports = router;
